<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>
        摄像头查看
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <!--    <link rel="stylesheet" href="${pageContext.request.contextPath }/statics/css/main.css" media="all">
        <link rel="stylesheet" href="${pageContext.request.contextPath }/statics/css/bootstrap.css">
        <script type="text/javascript" src="${pageContext.request.contextPath }/statics/lib/loading/okLoading.js"></script>-->
    <link rel="stylesheet" th:href="@{/layui-v2.6.8//layui/css/layui.css}"
          href="${pageContext.request.contextPath }/statics/layui-v2.6.8//layui/css/layui.css">
    <style>
        #zz {
            width: 1035px;
            height: 620px;
            border: 1px #5B6674 solid;
            background-color: #313F4A;
            margin: auto;
            margin-top: 25px;
            font-family: 微软雅黑;
        }

        #zz #s1, #s2, #s3, #s4 {
            margin-top: 5px;
            margin-left: 5px;
        }

        #s1 {
            width: 380px;
            height: 300px;
            float: left;
            background-color: #4A5662;
            position: relative;
        }

        #s2 {
            width: 380px;
            height: 300px;
            float: left;
            background-color: #4A5662;
            position: relative;
        }

        #s3 {
            width: 380px;
            height: 300px;
            float: left;
            background-color: #4A5662;
        }

        #s4 {
            width: 380px;
            height: 300px;
            float: left;
            background-color: #4A5662;
        }

        #s5 {
            margin-top: 5px;
            margin-right: 5px;
            color: #D3DBE1;
            width: 255px;
            height: 610px;
            float: right;
            background-color: #4A5661;
        }

        video {
            width: 375px;
            height: 250px;
            object-fit: fill;
        }

        .layui-table td, .layui-table th {
            font-size: 13px;
            text-align: center;
        }

        .layui-table-cell {
            padding: 0px;
            height: 18px;
            line-height: 15px;
        }

        .wenzi {
            position: absolute;
            float: left;
            color: white;
            width: 45px;
            height: 20px;
            text-align: center;
            z-index: 1;
            margin-left: 8px;
            margin-top: 5px;
        }

        .fontStyle {
            color: #019688;
            font-weight: bold;
        }

        .fontStyle2 {
            color: #FF5722;
            font-weight: bold;
        }

        .font {
            color: white;
            font-size: 18px;
            font-weight: bold
        }

        .font2 {
            /*padding: 6px;*/
            /*margin: 15px 0px;*/
            color: white;
            font-size: 15px;
            font-weight: bold;
            /*border: 1px solid red;*/
        }

        .time li {
            height: 20px;
            line-height: 20px;
        }

        #price {
            color: yellow;
            font-size: 25px;
            font-weight: bold;
        }

        .price {
            height: 30px;
            line-height: 30px;
        }

    </style>
</head>
<body style="text-align: center;background-color: #40454E">
<div class="x-nav" style="text-align: left;border:none">
            <span class="layui-breadcrumb">
              <a><cite>首页</cite></a>
              <a><cite>车辆管理</cite></a>
              <a><cite>摄像头查看</cite></a>
            </span>
    <a class="layui-btn layui-btn-primary layui-border-blue" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon"
                                                                        style="line-height:30px">ဂ</i></a>
</div>

<div id="zz">
    <div id="s1">
        <div class="wenzi"><span class="layui-badge-dot layui-bg-green"></span> 入口</div>
        <video id="video" autoplay></video>

        <p style="margin-top: 7px">
            <button class="layui-btn" style="height: 30px;padding: 0px 25px;line-height: 30px"
                    id="snap">拍摄
            </button>
        </p>
    </div>

    <div id="s2">
        <div class="wenzi"><span class="layui-badge-dot"></span> 出口</div>
        <video id="video2" autoplay></video>
        <p style="margin-top: 7px">
            <button class="layui-btn layui-btn-danger" style="height: 30px;padding: 0px 25px;line-height: 30px"
                    id="snap2">拍摄
            </button>
        </p>
    </div>
    <div id="s5">
        <fieldset class="layui-elem-field">
            <legend>停车场信息</legend>
            <div class="layui-field-box">
                <ol>
                    <li>场内总停车位：<span id="count" class="font">100</span></li>
                    <li>场内固定停车位：<span id="fixed2" class="font">100</span></li>
                    <li>当前临时车数量：<span id="temp" class="font">100</span></li>
                    <li>剩余临时停车位：<span id="surplus" class="font">100</span></li>
                    <!--<li>当前固定车数量：<span id="fixed" class="font">100</span></li>-->
                </ol>
            </div>
        </fieldset>
        <fieldset class="layui-elem-field">
            <legend>收费信息</legend>
            <div class="layui-field-box">
                <ol>
                    <li>操作员：<span class="font" th:text="${session.userSession.account}">${sessionScope.userSession.account}</span>
                    </li>
                    <!--<li>收费名称：停车场计费</li>-->
                    <li>临时车收费：<span class="font" id="money">3</span> /小时</li>
                    <li>优惠：<span class="font">30</span> 分钟内免费</li>
                    <!--<li>免费车次：0（辆）</li>
                    <li>手动放行：0（辆）</li>
                    <li>优惠车数量：0（辆）</li>-->
                    <li>共收金额：<span id="sum" class="font"></span>（元）</li>
                </ol>
            </div>
        </fieldset>

        <fieldset class="layui-elem-field">
            <legend>本次计费</legend>
            <div class="layui-field-box">
                <ol class="time">
                    <li>停车时间：<span id="in_date" class="font2"></span></li>
                    <li>离开时间：<span id="out_date" class="font2"></span></li>
                    <li>总时间：<span id="on_date" class="font2"></span></li>
                </ol>
            </div>
        </fieldset>
        <fieldset class="layui-elem-field layui-field-title">
            <legend>消费合计：</legend>
            <div class="layui-field-box">
                <dd style="font-size: 20px;color: yellow" class="price">收费：<span id="price"></span></dd>
            </div>
            <div class="layui-form-item" align="center">
                <button class="layui-btn layui-btn-radius" style="width: 115.6px">
                    <i class="layui-icon">&#x1005;</i>放 行
                </button>
                <div style="padding: 2px"></div>
                <button class="layui-btn layui-btn-radius layui-btn-danger"
                        onclick="veh_add('异常处理','exception','700','450')">
                    <i class="layui-icon">&#xe608;</i>异常处理
                </button>
            </div>
        </fieldset>
    </div>
    <div id="s3" style="color: black">
        <fieldset class="layui-elem-field layui-field-title">
            <legend style="color: #0F8980;font-size: 20px;font-weight: bold">入口历史信息</legend>
            <table id="in" lay-filter="test"></table>
        </fieldset>
    </div>
    <div id="s4" style="color: black">
        <fieldset class="layui-elem-field layui-field-title">
            <legend style="color: #FF5722;font-size: 20px;font-weight: bold">出口历史信息</legend>
            <table id="out" lay-filter="test"></table>
        </fieldset>
        <div style="display: none">
            <canvas id="canvas" width="380" height="250"></canvas>
            <!--<img id="tulip" src="/upload/404.png" alt="The Tulip" style="width: 375px;height: 250px;display: none"/>-->
            <p style="margin-top: 20px;display: none" id="Fe">
                                <span style="background-color: #0120B5;width: 150px;height: 150px;color: white;font-size: 25px;padding: 3px"
                                      id="license">京P JV676</span>
                <span style="color: #7A9F26">车辆类型：</span><span style="color: white" id="cartype">临时车</span>
                <span style="color: #FB8008;display: block;margin-top: 10px;font-weight: bolder;font-size: 18px"
                      id="show">减速慢行，一车一杠</span>
            </p>
        </div>
    </div>
</div>

<script th:src="@{/js/x-layui.js}" src="${pageContext.request.contextPath }/statics/js/x-layui.js"
        charset="utf-8"></script>
<script th:src="@{/layui-v2.6.8/layui/layui.js}"
        src="${pageContext.request.contextPath }/statics/layui-v2.6.8/layui/layui.js"></script>
<script th:src="@{/js/jquery.min.js}" src="${pageContext.request.contextPath }/statics/js/jquery.min.js"></script>
<script>
    //查询车位数量
    async function parkingCount(carState, personnelState) {
        $.ajax({
            url: "/parking/count",
            dataType: "json",
            data: {carState: carState, personnelState: personnelState},
            type: "GET",
            success: function (data) {
                if (personnelState == undefined) $("#count").text(data.data);
                else if (personnelState == 1) $("#surplus").text(data.data);
                else if (personnelState == 2) $("#fixed2").text(data.data);
            }
        })
    }

    parkingCount(undefined, undefined)
    parkingCount(1, 1)
    parkingCount(undefined, 2)

    //查询临时车位数量
    async function parkingCount2(carType) {
        $.ajax({
            url: "/parkingInmanage/count",
            dataType: "json",
            type: "GET",
            data: {carType: carType},
            success: function (data) {
                if (carType == "临时车") $("#temp").text(data.data);
                if (carType == "固定车") $("#fixed").text(data.data);
            }
        })
    }

    parkingCount2("临时车")
    parkingCount2("固定车")


    //查询共计金额
    function priceSum() {
        $.ajax({
            url: "/parkingOut/sum",
            dataType: "json",
            type: "GET",
            success: function (data) {
                $("#sum").text(data);
            }
        })
    }

    priceSum()

    //查询临时车收费
    function priceCount() {
        $.ajax({
            url: "/fee/carlist",
            dataType: "json",
            type: "GET",
            data: {feeNo: '922021120279280304'},
            success: function (data) {
                $("#money").text(data.data.records[0].feeMoney);
            }
        })
    }

    priceCount()

    function getJsonLength(jsonData) {  //计算车牌识别结果长度
        var jsonLength = 0;
        for (var item in jsonData) {
            jsonLength++;
        }
        return jsonLength;
    }

    // Converts canvas to an image  车牌识别接口
    function convertCanvasToImage(canvas) {
        var statu = 0;
        var licenseplate;
        var str = canvas.toDataURL("image/png");
        var imgData = str.replace("data:image/png;base64,", "");
        $.ajax({
            url: "getlicense",
            data: {imgData: imgData},
            type: "post",
            async: false,
            dataType: "json",
            success: function (data) {
                statu = getJsonLength(data);
                if (statu == 2) {
                    //获取车牌号
                    licenseplate = data.words_result.number
                } else {
                    layer.msg("没有识别到车牌号", {icon: 5, time: 3000})
                }
            }
        })
        return licenseplate;
    }

    var video = document.getElementById('video');
    // 得到摄像头api
    if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
        // 如果我们想要立即显示拍照，请设置 { video: true }
        navigator.mediaDevices.getUserMedia({video: true}).then(
            function (stream) {
                video.srcObject = stream;
                video.play();
            });
    }

    var video2 = document.getElementById('video2');
    // 得到摄像头api
    if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
        // 如果我们想要立即显示拍照，请设置 { video: true }
        navigator.mediaDevices.getUserMedia({video: true}).then(
            function (stream) {
                video2.srcObject = stream;
                video2.play();
            });
    }

    // 用于拍摄快照的元素,添加canvas里面
    var canvas = document.getElementById('canvas');
    var context = canvas.getContext('2d');

    layui.use(['table', 'layer'], function () {
        var table = layui.table;
        var layer = layui.layer;
        //第一个窗口
        table.render({
            elem: '#in'
            , width: 380
            , height: 260
            , url: '/parkingIn/list?size=100' //数据接口
            , page: false //开启分页
            , cols: [
                [ //表头
                    // {field: 'id', title: 'ID', width: 80, sort: true, fixed: 'left'},
                    {field: 'carNumber', title: '入口车辆', width: 90}
                    , {field: 'inDate', title: '进入时间', width: 130, sort: true}
                    , {field: 'carType', title: '车辆类型', width: 75}
                    , {field: 'parking', title: '车位', width: 63}
                ]
            ], parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.records //解析数据列表
                }
            }
        });

        //第二个窗口
        table.render({
            elem: '#out'
            , width: 380
            , height: 260
            , url: '/parkingOut/list?size=100' //数据接口
            , page: false //开启分页
            , cols: [
                [ //表头
                    // {field: 'id', title: 'ID', width: 80, sort: true, fixed: 'left'},
                    {field: 'carNumber', title: '出口车辆', width: 75}
                    , {field: 'inDate', title: '进入时间', width: 125, sort: true}
                    , {field: 'outDate', title: '离开时间', width: 125, sort: true}
                    , {field: 'money', title: '收费', width: 33}
                ]
            ], parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.records //解析数据列表
                }
            }
        });

        // 入口触发的照片
        document.getElementById("snap").addEventListener("click", function () {
            context.drawImage(video, 0, 0, 375, 250);

            var licenseplate_in = convertCanvasToImage(canvas);

            function existParking(licenseplate_in) {   //查询固定车辆表返回车辆对象
                $.ajax({
                    url: "/parkingIn/in",
                    data: {licensePlate: licenseplate_in},
                    type: "GET",
                    dataType: "json",
                    success: function (data) {
                        if (data.code == -1) {
                            layer.msg(data.msg, {icon: 5, time: 2000})
                        } else {
                            layer.msg("欢迎您：" + licenseplate_in + ",车位号是：" + data.parking, {icon: 1, time: 6000});
                            table.reload('in', {})
                            $("#Fe").show()
                            $("#license").text(data.licenseplate_in);
                            $("#cartype").text(data.carType);
                            setTimeout(function () {
                                var img = document.getElementById("tulip");
                                $("#Fe").hide()
                            }, 10000)

                            setTimeout(function () {
                                $("tbody:eq(0) tr:first-child").addClass("fontStyle");
                            }, 200)

                            setTimeout(function () {
                                $("tbody:eq(0) tr:first-child").removeClass();
                            }, 20000)
                            parkingCount(1, 1)
                            parkingCount2("临时车")
                            parkingCount2("固定车")
                        }
                    }
                })
            }

            existParking(licenseplate_in);  //车辆进入获取车牌号第一手操作
        });

        // 出口触发的照片
        document.getElementById("snap2").addEventListener("click", function () {
            context.drawImage(video2, 0, 0, 375, 250);

            var licenseplate_out = convertCanvasToImage(canvas);

            function existParking_inManage(licenseplate_out) {   //出口

                $.ajax({
                    url: "/parkingOut/out",
                    data: {licenseplate: licenseplate_out},
                    type: "GET",
                    dataType: "json",
                    success: function (data) {
                        if (data.code == -1) {
                            layer.msg(data.msg, {icon: 5, time: 2000})
                        }
                        else {
                            layer.msg("一路顺风：" + licenseplate_out);
                            console.info(data)
                            table.reload('out');
                            $("#Fe").show()
                            $("#license").text(data.data.carNumber);
                            $("#cartype").text(data.data.carType);

                            $("#in_date").text(data.data.inDate)
                            $("#out_date").text(data.data.outDate)
                            $("#on_date").text(data.data.onDate)
                            $("#price").text(data.data.price)

                            setTimeout(function () {
                                $("#Fe").hide()
                                $("#in_date").text("")
                                $("#out_date").text("")
                                $("#on_date").text("")
                                $("#price").text("")
                            }, 20000)

                            setTimeout(function () {
                                $("tbody:eq(1) tr:first-child").addClass("fontStyle2");
                            }, 200)
                            setTimeout(function () {
                                $("tbody:eq(1) tr:first-child").removeClass();
                            }, 20000)
                            parkingCount(1, 1)
                            parkingCount2("临时车")
                            parkingCount2("固定车")
                            priceCount()
                        }
                    }
                })
            }

            existParking_inManage(licenseplate_out);  //车辆出去获取车牌号第一手操作

        });
    });

    /*添加*/
    function veh_add(title, url, w, h) {
        x_admin_show(title, url, w, h);
    }
</script>

</body>
</html>